package org.grottarossa.tms;

import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.grottarossa.tms.ui.console.Console;
import org.grottarossa.tms.ui.console.main.MainConsoleFactory;
import org.grottarossa.tms.ui.console.main.MainConsoleView;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.RuntimeVariables;
import org.grottarossa.ui.core.i18n.DM;

import com.vaadin.Application;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.gwt.server.HttpServletRequestListener;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;

public class TMSHomeApplication extends Application implements HttpServletRequestListener
{
	private static final long serialVersionUID = 4132958172226158923L;
	protected ResourceBundle m_localizer = DM.instance().getBundle(Console.class.getPackage().getName() + ".CommonMessages");

	@Override
	public void init()
	{
		setTheme("psa_pv");
		setMainWindow(new Window("TMS - Task Management System"));

		try
		{
			String user = (String)((WebApplicationContext)getContext()).getHttpSession().getAttribute("user");
			String password = (String)((WebApplicationContext)getContext()).getHttpSession().getAttribute("psw");


			synchronized (TMSHomeApplication.class)
			{
				ApplicationSession appSession = new ApplicationSession(user, password, getMainWindow());
				appSession.initialize();

				MainConsoleFactory f = new MainConsoleFactory(appSession);
				MainConsoleView mc = f.createMainConsole();
				mc.setWidth(100, Sizeable.UNITS_PERCENTAGE);
				mc.setHeight(100, Sizeable.UNITS_PERCENTAGE);

				setLogoutURL("/PSAPV/Home");

		        getMainWindow().setContent(mc);
	        }
		}
		catch(Exception ex)
		{
			getMainWindow().showNotification(ex.toString());
			ex.printStackTrace();
		}


		/*HorizontalLayout l = new HorizontalLayout();


		try
		{
			Component c = (Component) LayoutFactory.instance().createLayout(
					"Allega", new Class[] { Record.class, Layout.class},  new Object[] { null, l} );

			getMainWindow().addComponent(c);
			getMainWindow().addComponent(l);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}*/
	}

	public void onRequestStart(HttpServletRequest request,
			HttpServletResponse response)
	{
		RuntimeVariables.PSAPV_Port = request.getLocalPort();
	}


	public void onRequestEnd(HttpServletRequest request,
			HttpServletResponse response) {

	}
}